#!/uar/bin/env bash
set -e
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :Python3 开启简单http server服务
# Platform :RedHatEL7.x Based Platform
# Version  :1.01
# Date     :2022-10-14
# Author   :mugoLH
# Contact  :hxsaj@126.com
# Company  :liando
# depend on:
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

# Python3 二进制位置
PYTHON3=

#  函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 提示
info_p(){  printf "\e[1;32;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}
tips_p(){  printf "\e[1;36;40m%-5s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}
error_p(){ printf "\e[1;35;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}

PythonOpenHttp(){
	PYTHON3=${PYTHON3}
	HTTP_DIR=${1}
	HTTP_PORT=${2}
	HTTP_IPv4_OR_6=${3}

	if [ -x ${PYTHON3} ] && [ ! -z ${PYTHON3} ];then
		if [ ! -z ${HTTP_DIR} ] ;then
			if [ -z ${HTTP_IPv4_OR_6} ] || [ ${HTTP_IPv4_OR_6} -eq 4 ];then
				${PYTHON3} -m http.server ${HTTP_PORT} --bind 0.0.0.0 -d ${HTTP_DIR} &
			elif [ ${HTTP_IPv4_OR_6} -eq 6 ] ;then
				${PYTHON3} -m http.server ${HTTP_PORT} --bind :: -d ${HTTP_DIR} &
			else
				error_p "[ ERROR ]" "${PYTHON3} 需要指定IPv4或v6."
			fi
		else
			error_p "[ ERROR ]" "未指定目录！"
		fi
	else
		error_p "[ ERROR ]" "${PYTHON3} 不是Python3二进制可执行文件！"
	fi
}

PythonOpenHttp ${1} ${2} ${3}
